package cn.lut.service.impl;

import cn.lut.dao.GoodsDao;
import cn.lut.dao.SaleDao;
import cn.lut.entity.Goods;
import cn.lut.entity.Sale;
import cn.lut.excep.NotEnoughException;
import cn.lut.service.BuyGoodsService;

/**
 * @user:dell
 * @date:2020年11月02 15:01
 */
public class BuyGoodsServiceImpl  implements BuyGoodsService {
private  SaleDao saleDao;
private  GoodsDao goodsDao;

    public SaleDao getSaleDao() {
        return saleDao;
    }

    public void setSaleDao(SaleDao saleDao) {
        this.saleDao = saleDao;
    }

    public GoodsDao getGoodsDao() {
        return goodsDao;
    }

    public void setGoodsDao(GoodsDao goodsDao) {
        this.goodsDao = goodsDao;
    }

    @Override
    public void buy(Integer goodsId, Integer nums) {
        System.out.println("buy方法的开始");
//        记录销售的信息，向sale表中添加记录
        Sale sale=new Sale(null,goodsId,nums);
        saleDao.insertSale(sale);
//        更新库存
        Goods goods=goodsDao.selectGoods(goodsId);
        if(goods== null){
            System.out.println("商品不存在");
            throw new NullPointerException("编号是"+goodsId+"的商品不存在");
        }else if(goods.getAmount()<nums){
            System.out.println("商品库存不足");
            throw new NotEnoughException("编号是"+goodsId+"的库存不足");
        }
//        修改库存
        Goods buyGoods = new Goods();
        buyGoods.setId(goodsId);
        buyGoods.setAmount(nums);
        goodsDao.updateGoods(buyGoods);
        System.out.println("=====buy方法的完成==========");
    }
}
